package com.example.service.impl;


import com.example.constant.sta;
import com.example.mapper.BrandMapper;
import com.example.mapper.DefectMapper;
import com.example.mapper.VerifyMapper;
import com.example.pojo.PageResult;
import com.example.pojo.VerifyResult;
import com.example.pojo.VerifyResultImg;
import com.example.service.VerifyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    private VerifyMapper  verifyMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private DefectMapper defectMapper;

    //补全verifyResult的信息
//    public VerifyResult completeVerifyResult(VerifyResult verifyResult){
//
//        verifyResult.setMachineID(verifyResult.getMachineID());
//
//        verifyResult.setVerifyTime(LocalDateTime.now());
//
//        int total = verifyResult.getVerifyResult().size();
//        int  success = 0;
//        for (String s : verifyResult.getVerifyResult()) {
//            if (s.equals("1")) {
//                success++;
//            }
//        }
//        verifyResult.setSuccessRate((float)success/total);
//
//        verifyMapper.insert(verifyResult);
//
//
//        VerifyResultImg verifyResultImg = new VerifyResultImg();
//        Integer verifyresultID = verifyMapper.findNewest().getID();
//
//        for (int i = 0; i < verifyResult.getFileList().size(); i++) {
//            verifyResultImg.setVerifyResultID(verifyresultID);
//            verifyResultImg.setFileName(verifyResult.getFileList().get(i));
//            verifyResultImg.setVerifyResult(Integer.valueOf(verifyResult.getVerifyResult().get(i)));
//            verifyResultImg.setDefectID(verifyResult.getDefectID().get(i));
//            verifyResultImg.setBrandID(verifyResult.getBrandID().get(i));
//            verifyResultImg.setDefectName(verifyResult.getDefectName().get(i));
//            verifyResultImg.setBrandName(verifyResult.getBrandName().get(i));
//            verifyMapper.insertImg(verifyResultImg);
//        }
//
//        return verifyResult;
//    }

    public VerifyResult completeVerifyResult(VerifyResult verifyResult) {
        // 设置机器ID和验证时间
        verifyResult.setMachineID(verifyResult.getMachineID());
        verifyResult.setVerifyTime(LocalDateTime.now());

        // 计算成功率
        int total = verifyResult.getVerifyResult().size();
        int success = 0;
        for (String s : verifyResult.getVerifyResult()) {
            if (s.equals("1")) {
                success++;
            }
        }
        verifyResult.setSuccessRate((float) success / total);

        // 查询 verifyresult 表的最大 ID 并加 1
        Integer maxVerifyResultID = verifyMapper.findMaxID();
        Integer newVerifyResultID = (maxVerifyResultID == null ? 0 : maxVerifyResultID) + 1;

        // 先插入 verifyresultimg 表，使用 newVerifyResultID
        VerifyResultImg verifyResultImg = new VerifyResultImg();
        for (int i = 0; i < verifyResult.getFileList().size(); i++) {
            verifyResultImg.setVerifyResultID(newVerifyResultID);
            verifyResultImg.setFileName(verifyResult.getFileList().get(i));
            verifyResultImg.setVerifyResult(Integer.valueOf(verifyResult.getVerifyResult().get(i)));
            verifyResultImg.setDefectID(verifyResult.getDefectID().get(i));
            verifyResultImg.setBrandID(verifyResult.getBrandID().get(i));
            verifyResultImg.setDefectName(verifyResult.getDefectName().get(i));
            verifyResultImg.setBrandName(verifyResult.getBrandName().get(i));
            verifyMapper.insertImg(verifyResultImg);
        }

        // 插入 verifyresult 表
        verifyResult.setID(newVerifyResultID); // 确保 verifyResult 的 ID 也设置为新的 ID
        verifyMapper.insert(verifyResult);

        return verifyResult;
    }

    //添加verifyResult
    public void insert(VerifyResult verifyResult){
        verifyMapper.insert(verifyResult);
    }

    //添加verifyResultImg
    public void insertImg(VerifyResultImg verifyResultImg){
        verifyMapper.insertImg(verifyResultImg);
    }


    // 查询所有验证结果
    public List<VerifyResult> findAll(){
        return verifyMapper.findAll();
    }

    //获取最新的验证结果
    public VerifyResult findNewest(){
        return verifyMapper.findNewest();
    }

    //根据verifyresult表的id从verifyresultimg表中查询与verifyresultid相同的数据
    public List<VerifyResultImg> findByVerifyResultID(Integer ID){
        List<VerifyResultImg> verifyResultImgList = verifyMapper.findByVerifyResultID(ID);
        //遍历列表，补全brandName和defectName
        for (VerifyResultImg verifyResultImg : verifyResultImgList) {
            verifyResultImg.setBrandName(brandMapper.getBrandNameById(verifyResultImg.getBrandID()));
            verifyResultImg.setDefectName(defectMapper.getDefectNameById(verifyResultImg.getDefectID()));
        }

        return verifyResultImgList;
    }


    //分页查询验证结果
    public PageResult page(Integer page, Integer pageSize){
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        List<VerifyResult> list = verifyMapper.findAllF();
        Page<VerifyResult> p = (Page<VerifyResult>) list;
        return new PageResult(p.getTotal(), p.getResult());
    }

    //获取所有不同machineID的最新验证结果
    public List<VerifyResult> getAllNewest(){
        return verifyMapper.getAllNewest();
    }


}
